var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function searchList(pageNum) {
    if($('#topicType').val()==""){
        $.Huimodalalert('请选择类型！',2000);
        return false;
    }
    if($('#sendCouponType').val()==""){
        $.Huimodalalert('请选择发券方式！',2000);
        return false;
    }
    if($('#logmax').val()==""||$('#logmin').val()==""){
        $.Huimodalalert('请选择发放日期！',2000);
        return false;
    }

    $.ajax({
        url: root.couponInter.coupon_getCouponInfoExpiredList,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "sendCouponType":$('#sendCouponType').val(),
            "sendCustType":$('#custType').val(),
            "custArea":$('#custArea').val(),
            "custName":$('#custName').val(),
            "couponCode":$('#couponCode').val(),
            "useStatus":$('#useStatus').val(),
            "topicType":$('#topicType').val(),
            "topicCode":$('#topicCode').val(),
            "topicName":$('#topicName').val(),
            "orderId":$('#orderId').val(),
            "sendCouponDept":$('#sendCouponDept').val(),
            "sendCouponUser":$('#sendCouponUser').val(),
            "inputStarTime":$('#logmin').val(),
            "inputEndTime":$('#logmax').val(),
            "sendReasonDutyDept":$('#sendReasonDutyDept').val(),
            "sendCouponReason":$('#sendCouponReason').val(),
            "pageNum": pageNum,
            "pageSize": 10,
        }),
        success: function (data) {
            console.log(data);
            couponlist(data.result);
            $('.M-box3').pagination({
                pageCount: data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url: root.couponInter.coupon_getCouponInfoExpiredList,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "sendCouponType":$('#sendCouponType').val(),
                            "sendCustType":$('#custType').val(),
                            "custArea":$('#custArea').val(),
                            "custName":$('#custName').val(),
                            "couponCode":$('#couponCode').val(),
                            "useStatus":$('#useStatus').val(),
                            "topicType":$('#topicType').val(),
                            "topicCode":$('#topicCode').val(),
                            "topicName":$('#topicName').val(),
                            "orderId":$('#orderId').val(),
                            "sendCouponDept":$('#sendCouponDept').val(),
                            "sendCouponUser":$('#sendCouponUser').val(),
                            "inputStarTime":$('#logmin').val(),
                            "inputEndTime":$('#logmax').val(),
                            "sendReasonDutyDept":$('#sendReasonDutyDept').val(),
                            "sendCouponReason":$('#sendCouponReason').val(),
                            "pageNum": api.getCurrent(),
                            "pageSize": 10,
                        }),
                        success: function (result) {
                            console.log(result)
                            couponlist(result.result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
function couponlist(data){
    dataSource.splice(0,dataSource.length);//清空数组
    $.each(data, function (i, field) {
        var rowData = new Array();
        rowData.push(field.couponCode);
        rowData.push(field.topicType);
        rowData.push(field.custArea);
        rowData.push(field.custName);
        rowData.push(field.sendCustType);//4
        rowData.push(field.orderCustName);
        rowData.push(field.sendCouponReason);
        rowData.push(field.sendCouponDept);//7
        rowData.push(field.sendCouponUser);//8
        rowData.push(field.auditTime);
        rowData.push(field.auditPerson);
        rowData.push(field.orderId);
        rowData.push(field.useStatus);
        rowData.push(field.topicName);//13
        rowData.push(field.topicCode);//14
        rowData.push(field.amount);//15
        rowData.push(field.topicId);
        rowData.push(field.sendCouponType);
        rowData.push(field.customerType);
        rowData.push(field.sendCondition);
        rowData.push(field.custType);
        rowData.push(field.bindTime);

        dataSource.push(rowData);
    });
    initDataTable(dataSource);
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "order": [[ 10, "desc" ]],
            "autoWidth": false,
            "ordering": true,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        if(row[1]==10){
                            var val = '<a onclick="detail(\'发券审核\',\'coupon-topic-detail.html\',\''+row[16]+'\')">满减券  '+row[14]+'<br>'+row[13]+'<br>面额：'+row[15]+'元</a>';
                            return val
                        }
                        else  if(row[1]==20){
                            var val = '<a onclick="detail(\'发券审核\',\'coupon-topic-detail.html\',\''+row[16]+'\')">运费券  '+row[14]+'<br>'+row[13]+'<br>面额：'+row[15]+'元</a>';
                            return val
                        }
                            return val;
                    },
                    "targets":1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        if (row[17] == 1) {
                            val = '手动发券'
                        }else if (row[17] == 2 && row[19] == 0) {
                            val = '自动发券'
                        }else if (row[17] == 2 && row[19] == 1) {
                            val = '自动发券</br> 新用户注册'
                        }else if (row[17] == 2 && row[19] == 2) {
                            val = '自动发券</br> 满足订单金额条件</br>'
                        }else if (row[17] == 3 ) {
                            val = '客户领取发券'
                        }else if (row[17] == 4) {
                            val = '抽奖发送'
                        }
                        return val;
                    },
                    "targets":4
                },
                {
                    "render": function(data,type,row,meta){
                        var val = '';
                        if(row[20]==11){
                            val = '签约客户(非分销)';
                        }else if(row[20]==12){
                            val = '签约客户(分销)';
                        }else if(row[20]==20){
                            val = '普通客户';
                        }else{
                            val = '认证客户';
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[5];
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        if(row[6]==11){
                            val='质量问题'
                        } else if(row[6]==12){
                            val='无货问题'
                        } else if(row[6]==21){
                            val='配送延迟'
                        } else if(row[6]==22){
                            val='状态维护错误'
                        } else if(row[6]==23){
                            val='私自拒收'
                        } else if(row[6]==24){
                            val='包装/商品破损'
                        } else if(row[6]==25){
                            val='违规收费'
                        } else if(row[6]==26){
                            val='服务态度差'
                        } else if(row[6]==27){
                            val='错漏发'
                        } else if(row[6]==31){
                            val='退款时效慢'
                        } else if(row[6]==32){
                            val='发票信息有误'
                        } else if(row[6]==33){
                            val='退款金额有误'
                        } else if(row[6]==34){
                            val='开具发票时间延迟'
                        } else if(row[6]==41){
                            val='商品参数错误'
                        } else if(row[6]==42){
                            val='价格错误'
                        } else if(row[6]==43){
                            val='活动与实际不符'
                        } else if(row[6]==44){
                            val='活动优惠券补发'
                        } else if(row[6]==45){
                            val='活动优惠券发放'
                        } else if(row[6]==51){
                            val='特殊售后问题'
                        } else if(row[6]==52){
                            val='提升客户体验'
                        } else if(row[6]==53){
                            val='承诺错误'
                        } else if(row[6]==54){
                            val='售后时间长'
                        } else if(row[6]==61){
                            val='售后运费补偿'
                        } else if(row[6]==62){
                            val='错发运费补偿'
                        } else if(row[6]==71){
                            val='订单未流转'
                        } else if(row[6]==72){
                            val='活动未绑定优惠券、赠品'
                        } else if(row[6]==73){
                            val='系统自动取消'
                        } else if(row[6]==74){
                            val='页面缓存问题'
                        }
                        return val;
                    },
                    "targets":7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7];
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":8
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[8];
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":9
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[21];
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":10
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        if(row[12]==10){
                            val='未开始'
                        }else if(row[12]==20) {
                            val = '可使用'
                        }else if(row[12]==30) {
                            val = '已使用'
                        }else if(row[12]==40) {
                            val = '已过期'
                        }else if(row[12]==50) {
                            val = '作废'
                        }
                        return val;
                    },
                    "targets":11
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[11];
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":12
                },
            ]
        });
    }
}

function confirm(title) {
   layer.open({
        type: 2,
        skin: 'layui-layer-rim', //加上边框
        area: ['580px', '640px'], //宽高
        title: title,
        shadeClose:true,
        content:'coupon-use-query-putout.html'
    });
}
function detail(title,url,topicId) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url +'?topicId='+topicId
    });
    layer.full(index);
}
var flag1 = true;
$('#sendCouponDept').on('compositionstart',function(){
    flag1 = false;
})
$('#sendCouponDept').on('compositionend',function(){
    flag1 = true;
})
$('#sendCouponDept').on('input',function(){
    var _this = this;
    setTimeout(function(){
        if(flag1){
            var value=$(_this).val();
            $('li').remove();
            $.ajax({
                url: root.accessInter.access_department_listDepartment,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                data:{
                    "keywords":value
                },
                success: function (data) {
                    console.log(data);
                    if(value!='')
                    {
                        $(".deptlist").removeClass('hide');
                        for (var i in data.bianmaList) {
                            $("#deptList").append('<li id="'+data.bianmaList[i].BIANMA+'" class="depts">' + data.bianmaList[i].DEPARTMENT_NAME + '</li>');
                        }
                    }
                    else{
                        document.getElementsByClassName('deptlist')[0].classList.add('hide');
                    }
                    $('.depts').on('click',function () {
                        $('#sendCouponDept').val(this.innerHTML);
                    })
                }
            });
        }
    },0)
})

var flag2 = true;
$('#sendCouponUser').on('compositionstart',function(){
    flag2 = false;
})
$('#sendCouponUser').on('compositionend',function(){
    flag2 = true;
})
$('#sendCouponUser').on('input',function(){
    var _this = this;
    setTimeout(function(){
        if(flag2){
            var value=$(_this).val();
            $('li').remove();
            $.ajax({
                url: root.accessInter.access_listUsername,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                data:{
                    "keywords":value
                },
                success: function (data) {
                    console.log(data.userName);
                    if(value!='')
                    {
                        $(".userlist").removeClass('hide');
                        for (var i in data.userName) {
                            $("#userList").append('<li id="'+data.userName[i].USER_ID+'" class="users">'+data.userName[i].NAME+'</li>');
                        }
                    }
                    else{
                        document.getElementsByClassName('userlist')[0].classList.add('hide');
                    }
                    $('.users').on('click',function () {
                        $('#sendCouponUser').val(this.innerHTML);
                    })
                }
            });
        }
    },0)
})
$(document).click(function(e) {
    var target = $(e.target);
    if(target[0].id!='sendCouponDept'){
        $('.depts').remove();
        $('.deptlist').addClass('hide');
    }
    if(target[0].id!='sendCouponUser'){
        $('.users').remove();
        $('.userlist').addClass('hide');
    }

})
//发券原因二级联动
var oldVal ='';
function sendDeptChange(Val){
    if(oldVal!=Val){
        $('#sendCouponReason').val('')
    }
    oldVal=Val;
    console.log(Val);
    var option = document.getElementsByClassName('options');
    console.log(option);
    switch (parseInt(Val)) {
        case 1:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value==11||option[i].value==12){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 2:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>20&&option[i].value<28){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 3:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>30&&option[i].value<35){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 4:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>40&&option[i].value<49){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 5:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>50&&option[i].value<59){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 6:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>60&&option[i].value<69){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
        case 7:
            for(var i=0 ; i<option.length ;i++)
            {
                if(option[i].value>70&&option[i].value<79){
                    console.log(option[i]);
                    option[i].classList.remove('hide');
                }
                else{
                    option[i].classList.add('hide');
                }
            }
            break;
    }
}